package com.vegas.netspider.apps.parser.sport.basketball;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.vegas.netspider.apps.parser.WebParser;
import com.vegas.netspider.dto.MatchScore;
import com.vegas.netspider.dto.SportMatchUtil;

public class ScoreParser implements WebParser<List<MatchScore>> {

	private Pattern bkScorePattern=Pattern.compile("<tr class=\"b_cen\">\\s*?<td>(.*?)<br>(.*?)</td>\\s*?<td>(.*?)<br>(.*?)</td>\\s*?<td align=\"left\">(.*?)<br>(.*?)</td>\\s*?<td><b><font color=\"#CC0000\">(.*?)<br>(.*?)</font></b></td>\\s*?<td><b><font color=\"#CC0000\">(.*?)<br>(.*?)</font></b></td>\\s*?<td><b><font color=\"#CC0000\">(.*?)<br>(.*?)</font></b></td>\\s*?<td><b><font color=\"#CC0000\">(.*?)<br>(.*?)</font></b></td>\\s*?<td><b><font color=\"#CC0000\">(.*?)<br>(.*?)</font></b></td>\\s*?<td><b><font color=\"#CC0000\">(.*?)<br>(.*?)</font></b></td>\\s*?<td><b><font color=\"#CC0000\">(.*?)<br>(.*?)</font></b></td>\\s*?<td><b><font color=\"#CC0000\">(.*?)<br>(.*?)</font></b></td>");
	@Override
	public List<MatchScore> parse(String res) {
		if(res==null||res.equals("")){
			return null;
		}
		Matcher matcher = bkScorePattern.matcher(res);
		
		List<MatchScore> list = new ArrayList<MatchScore>();
		while(matcher.find()){
			String date = matcher.group(1);
			String time = matcher.group(2);
			
			long playTime = SportMatchUtil.parsePlayTime(date+" "+time);
			
			String htmid = matcher.group(3);
			Integer htMatchId = Integer.valueOf(htmid);
			String gtmid = matcher.group(4);
			Integer gtMatchId = Integer.valueOf(gtmid);
			
			String htName = matcher.group(5);
			String gtName = matcher.group(6);
			
			String htQuarter1Result= matcher.group(7);
			String htQuarter2Result= matcher.group(9);
			String htQuarter3Result= matcher.group(11);
			String htQuarter4Result= matcher.group(13);
			
			String ctQuarter1Result= matcher.group(8);
			String ctQuarter2Result= matcher.group(10);
			String ctQuarter3Result= matcher.group(12);
			String ctQuarter4Result= matcher.group(14);
			
			String htHalf1Result= matcher.group(15);
			String htHalf2Result= matcher.group(17);
			String ctHalf1Result= matcher.group(16);
			String ctHalf2Result= matcher.group(18);
			
			String htExtraResult= matcher.group(19);
			String ctExtraResult= matcher.group(20);
			
			String htFinalResult= matcher.group(21);
			String ctFinalResult= matcher.group(22);
			
			
			MatchScore ms = new MatchScore();
			ms.setPlayTime(playTime);
			ms.setHomeTeamName(htName);
			ms.setHomeTeamId(htMatchId);
			ms.setGuestTeamName(gtName);
			ms.setGuestTeamId(gtMatchId);
			
			ms.setHtFirstHalfScore(htHalf1Result);
			ms.setHtSecondHalfScore(htHalf2Result);
			ms.setHtFullSocre(htFinalResult);
			
			ms.setGtFirstHalfScore(ctHalf1Result);
			ms.setGtSecondHalfScore(ctHalf2Result);
			ms.setGtFullSocre(ctFinalResult);
			
			ms.setHtExtraScore(htExtraResult);
			ms.setGtExtraScore(ctExtraResult);
			
			ms.setHtGuarter1Score(htQuarter1Result);
			ms.setHtGuarter2Score(htQuarter2Result);
			ms.setHtGuarter3Score(htQuarter3Result);
			ms.setHtGuarter4Score(htQuarter4Result);
			
			ms.setGtGuarter1Score(ctQuarter1Result);
			ms.setGtGuarter2Score(ctQuarter2Result);
			ms.setGtGuarter3Score(ctQuarter3Result);
			ms.setGtGuarter4Score(ctQuarter4Result);
			
			String existsMore = matcher.group(11);
			if(existsMore.indexOf("MORE")>-1){
				ms.setExistsMore(true);
			}
			list.add(ms);
		}
		return list;
	}

}
